package com.lekang.system.service.impl;

import java.util.List;
import com.lekang.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.system.mapper.NursingItemMapper;
import com.lekang.system.mapper.NursingItemPlanBindingMapper;
import com.lekang.system.domain.NursingItem;
import com.lekang.system.service.INursingItemService;
import com.lekang.common.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 护理项目Service业务层处理
 *
 *
 * @date 2025-10-14
 */
@Service
public class NursingItemServiceImpl implements INursingItemService
{
    private static final Logger log = LoggerFactory.getLogger(NursingItemServiceImpl.class);
    
    @Autowired
    private NursingItemMapper nursingItemMapper;
    
    @Autowired
    private NursingItemPlanBindingMapper nursingItemPlanBindingMapper;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingItem selectNursingItemById(Long id)
    {
        return nursingItemMapper.selectNursingItemById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingItem 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingItem> selectNursingItemList(NursingItem nursingItem)
    {
        return nursingItemMapper.selectNursingItemList(nursingItem);
    }

    /**
     * 新增护理项目
     *
     * @param nursingItem 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingItem(NursingItem nursingItem)
    {
        nursingItem.setCreateTime(DateUtils.getNowDate());
        // 名称唯一性校验（未删除的记录）
        NursingItem query = new NursingItem();
        query.setName(nursingItem.getName());
        query.setIsDeleted(0L);
        List<NursingItem> exists = nursingItemMapper.selectNursingItemList(query);
        if (exists != null && !exists.isEmpty()) {
            throw new ServiceException("护理项目名称已存在，请更换名称");
        }

        return nursingItemMapper.insertNursingItem(nursingItem);
    }

    /**
     * 修改护理项目
     *
     * @param nursingItem 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingItem(NursingItem nursingItem)
    {
        // 先更新护理项目本身
        int result = nursingItemMapper.updateNursingItem(nursingItem);
        
        // ✅ 如果状态发生改变，同步更新中间表中该护理项目的状态
        if (result > 0 && nursingItem.getStatus() != null) {
            try {
                String statusText = nursingItem.getStatus() == 1 ? "启用" : "禁用";
                log.info("护理项目ID={} 状态更新为：{}，同步更新中间表", nursingItem.getId(), statusText);
                
                int updateCount = nursingItemPlanBindingMapper.updateStatusByNursingItemId(
                    nursingItem.getId(), 
                    statusText
                );
                
                log.info("已同步更新 {} 条护理计划绑定记录的状态", updateCount);
            } catch (Exception e) {
                log.error("同步更新中间表状态失败", e);
                // 不抛出异常，避免影响护理项目本身的更新
            }
        }
        
        return result;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingItemByIds(Long[] ids)
    {
        return nursingItemMapper.deleteNursingItemByIds(ids);
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingItemById(Long id)
    {
        return nursingItemMapper.deleteNursingItemById(id);
    }
}
